﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using ThresholdAssistant.Autofac;
using ThresholdAssistant.DataContract.Result;
using ThresholdAssistant.Tools;
using ThresholdAssistant.WebAPI.Models;
using ThresholdAssistant.WebAPI.Models.Enums;
using ThresholdAssistant.WebAPI.Repository;

namespace ThresholdAssistant.WebAPI.Service
{
    /// <summary>
    /// 参数服务
    /// </summary>
    public class ConfigService : BaseService, IBaseAutofac
    {
        /// <summary>
        /// 参数仓储
        /// </summary>
        public ConfigRepository ConfigRepository { get; set; }
        /// <summary>
        /// 数据仓储
        /// </summary>
        public RecordRepository RecordRepository { get; set; }

        /// <summary>
        /// 初始化
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public async Task<ContractResult<bool>> InitAsync(GetThresholdListParam param)
        {
            var result = new ContractResult<bool>();

            var records = await GetListAsync(param);
            if (records.Data.IsNotNull() && records.Data.Any())
                return result;

            var list = new List<Config>();
            list.Add(new Config() { GroupId = param.GroupId, ConfigType = ConfigTypeEnum.IsWeChatNotice, ConfigName = "微信通知", ConfigValue = "1", ControlType = "switch", ControlIcon = "notification" });
            list.Add(new Config() { GroupId = param.GroupId, ConfigType = ConfigTypeEnum.IsMobileSMS, ConfigName = "短信通知", ConfigValue = "0", ControlType = "switch", ControlIcon = "sound" });
            list.Add(new Config() { GroupId = param.GroupId, ConfigType = ConfigTypeEnum.DataClearType, ConfigName = "数据自动清空", ConfigValue = "0", Remark = "不清空", ControlType = "select", ControlIcon = "chart" });
            list.Add(new Config() { GroupId = param.GroupId, ConfigType = ConfigTypeEnum.ClearUp, ConfigName = "立即清空", ConfigValue = "0", ControlType = "switch", ControlIcon = "app" });
            await ConfigRepository.AddListAsync(list);

            return result.SetSuceccful(true);
        }

        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public async Task<ContractResult<IEnumerable<ConfigData>>> GetListAsync(GetThresholdListParam param)
        {
            var result = new ContractResult<IEnumerable<ConfigData>>();
            var list = await ConfigRepository.GetListByGroupIdAsync(param.GroupId);
            result.Data = list?.Select(x => new ConfigData
            {
                Id = x.Id,
                GroupId = x.GroupId,
                ConfigName = x.ConfigName,
                ConfigValue = x.ConfigValue,
                SwitchValue = x.ConfigValue == "1",
                ConfigType = x.ConfigType,
                Remark = x.Remark,
                ControlType = x.ControlType,
                ControlIcon = x.ControlIcon,
            })?.ToList();
            return result.SetSuceccful();
        }

        /// <summary>
        /// 单个
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public async Task<ContractResult<ConfigData>> GetInfoAsync(long id)
        {
            var result = new ContractResult<ConfigData>();
            var model = await ConfigRepository.GetInfoAsync(id);
            if (model != null)
            {
                result.Data = new ConfigData
                {
                    Id = model.Id,
                    GroupId = model.GroupId,
                    ConfigName = model.ConfigName,
                    ConfigValue = model.ConfigValue,
                    ConfigType = model.ConfigType,
                    Remark = model.Remark,
                    ControlType = model.ControlType,
                    ControlIcon = model.ControlIcon,
                };
            }
            return result.SetSuceccful();
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        public async Task<ContractResult> UpdateAsync(UpdateConfigParam param)
        {
            var result = new ContractResult();

            var updlist = new List<Config>();
            var config = new Config();
            param.List.ForEach(async x =>
            {
                updlist.Add(new Config()
                {
                    Id = x.Id,
                    GroupId = x.GroupId,
                    ConfigValue = x.ConfigValue,
                    ConfigType = x.ConfigType,
                    UpdateTime = DateTime.Now
                });

                //bool转value
                config = updlist[updlist.Count - 1];
                if (x.ControlType == "switch")
                {
                    if (x.SwitchValue)
                        config.ConfigValue = "1";
                    else
                        config.ConfigValue = "0";
                }

                //立即清空
                if (config.ConfigType == ConfigTypeEnum.ClearUp && config.ConfigValue == "1")
                {
                    await RecordRepository.DeletesByGroupIdAsync(config.GroupId);
                    config.ConfigValue = "0";
                }
            });
            await ConfigRepository.UpdateListAsync(updlist);

            return result.SetSuceccful();
        }
    }
}
